package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.InventoryBean;
import com.project.bean.InventoryInfoBean;
import com.project.bean.ProcureBean;
import com.project.mapper.IInventoryInfoMapper;
import com.project.mapper.IInventoryMapper;
import com.project.mapper.IProcureMapper;
import com.project.service.IProcureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class ProcureServiceImp implements IProcureService {
    @Autowired
    private IInventoryMapper inventoryMapper;
    @Autowired
    private IInventoryInfoMapper infoMapper;
    @Autowired
    private IProcureMapper procureMapper;


    @Override
    public Boolean updateRemindNum(String productNumId, Integer newNum) {
        InventoryBean inventoryBean = inventoryMapper.selectById(productNumId);
        if (inventoryBean != null){
            inventoryBean.setRemindNum(newNum);
            inventoryMapper.updateById(inventoryBean);
            return true;
        }
        return false;
    }
    @Override
    public String updateProductNum(String productId, int newProductNum) {//修改采购商品数量
        if (productId ==null || productId ==""){//商品编号为空返回
            return "商品编号为空";
        }
        if (newProductNum<0){//修改数量不能为负数
            return "商品数量应大于0";
        }
        //通过商品编号查询商品
        QueryWrapper<ProcureBean> qw=new QueryWrapper<>();
        qw.eq("productId",productId);
        ProcureBean procureBean = procureMapper.selectOne(qw);
        if (procureBean ==null){//判断商品是否为空
            return "未查询到商品，请确认商品编号是否正确";
        }
        //修改商品数量
        procureBean.setNum(newProductNum);
        procureMapper.updateById(procureBean);
        return "ok";
    }

    @Override
    public IPage<ProcureBean> findAllProcure(int pageNO) {
        return procureMapper.selectPage(new Page<>(pageNO,3),null);
    }

    @Override
    public List<ProcureBean> findAllProcureList() {
        QueryWrapper<ProcureBean> queryWrapper=new QueryWrapper<>();
        return procureMapper.selectList(queryWrapper);
    }

    @Override
    public void del(int id) {
        procureMapper.deleteById(id);
    }

    @Override
    public Boolean addProcureProduct() {
        //先删除，在自动添加
        procureMapper.delete(null);

        List<InventoryInfoBean> infoBeanProcure =  new ArrayList<>();
        //产品表中查询到低于阈值的产品
        List<InventoryBean> inventoryBeans = inventoryMapper.findProcureInventory();
        //根据查询出来的产品，查询对应的所有商品
        for (InventoryBean inventoryBean : inventoryBeans){
            QueryWrapper<InventoryInfoBean> infoQw = new QueryWrapper<InventoryInfoBean>();
            infoQw.eq("fk_productNum",inventoryBean.getProductNum());
            List<InventoryInfoBean> infoBeans = infoMapper.selectList(infoQw);
            infoBeanProcure.addAll(infoBeans);
        }
        //先判断预存表中，是否已经存在商品，无，则将查询出来的商品添加到预采购表中，有则不添加
        List<ProcureBean> procureBeans = procureMapper.selectList(null);
        Set<String> productIds = procureBeans.stream()
                .map(ProcureBean::getProductId)
                .collect(Collectors.toSet());
        for (InventoryInfoBean inventoryInfoBean : infoBeanProcure) {
            if (!productIds.contains(inventoryInfoBean.getProductId())) {
                ProcureBean procureBean1 = new ProcureBean();
                BeanUtils.copyProperties(inventoryInfoBean, procureBean1);
                procureMapper.insert(procureBean1);
            }
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean addSdProcureProduct(String productId) {
        // 先判断库存表中是否存在该商品
        QueryWrapper<InventoryInfoBean> infoQw = new QueryWrapper<>();
        infoQw.eq("productId", productId);
        InventoryInfoBean infoBean = infoMapper.selectOne(infoQw);
        if (infoBean == null) {
            throw new RuntimeException("库存表中不存在该商品：" + productId);
        }

        // 判断采购表中是否已存在该商品，如果不存在，则添加
        QueryWrapper<ProcureBean> procureQw = new QueryWrapper<>();
        procureQw.eq("productId", productId);
        Long count = procureMapper.selectCount(procureQw);
        if (count == 0) {
            ProcureBean procureBean = new ProcureBean();
            BeanUtils.copyProperties(infoBean, procureBean);
            procureMapper.insert(procureBean);
            return true;
        } else {
            return false;
        }
    }
}
